package com.ourteam.product.requirement.api;

import com.ourteam.frame.web.AbstractTreeNodeBuilder;
import com.ourteam.product.requirement.dao.ProductDefineDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductVersionDAOQueryBean;
import com.ourteam.product.requirement.domain.ProductCatalogBean;
import com.ourteam.product.requirement.domain.ProductDefineBean;
import com.ourteam.product.requirement.service.IProductCatalogService;
import com.ourteam.product.requirement.service.IProductService;
import com.ourteam.product.requirement.service.IProductVersionService;
import com.ourteam.product.requirement.service.ProductCatalogServiceFactory;
import com.ourteam.product.requirement.service.ProductServiceFactory;
import com.ourteam.product.requirement.service.ProductVersionServiceFactory;

public class ProductDefineSubNodeBuilder extends AbstractTreeNodeBuilder<ProductCatalogBean, ProductDefineBean> {

	private static final IProductService PRODUCT_SERVICE = ProductServiceFactory.getProductService();

	private static final IProductVersionService PRODUCT_VERSION_SERVICE = ProductVersionServiceFactory
			.getProductVersionService();

	private static final IProductCatalogService PRODUCT_CATALOG_SERVICE = ProductCatalogServiceFactory
			.getProductCatalogService();

	@Override
	public ProductDefineBean[] getTreeNodeObjects(ProductCatalogBean parentObject) throws Exception {
		ProductDefineDAOQueryBean queryBean = new ProductDefineDAOQueryBean();

		//queryBean.createCriteria().andCatalogIdEqualTo(parentObject.getId());

		return PRODUCT_SERVICE.queryProductDefines(queryBean);
	}

	@Override
	public Object getNodeId(ProductDefineBean childObject) throws Exception {
		return childObject.getId();
	}

	@Override
	public boolean isLeaf(ProductDefineBean childObject) throws Exception {
		return false;
	}

	@Override
	public int getChildCount(ProductDefineBean childObject) throws Exception {

		ProductVersionDAOQueryBean queryBean = new ProductVersionDAOQueryBean();

		queryBean.createCriteria().andProductDefineIdEqualTo(childObject.getId());

		return PRODUCT_VERSION_SERVICE.queryProductVersionCount(queryBean);
	}

	@Override
	public String getNodeDefineId(ProductDefineBean childObject) throws Exception {
		return "productDefine";
	}

	@Override
	public ProductCatalogBean converToParentObject(Object parent) throws Exception {
		return null;//PRODUCT_CATALOG_SERVICE.getProductCatalogById(Long.parseLong(parent.toString()));
	}

}
